package com.skivingcloud.admin.sys.controller;

import com.mybatisflex.core.paginate.Page;
import com.skivingcloud.admin.sys.entity.SysMenu;
import com.skivingcloud.admin.sys.service.SysMenuService;
import com.skivingcloud.common.utils.ReturnT;
import com.skivingcloud.common.utils.UuidUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author skivingcloud
 * @since 2023-08-28
 */
@Tag(name = "SysMenuController", description = "菜单管理API")
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {
    private static final Logger log = LoggerFactory.getLogger(SysMenuController.class);
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 添加。
     *
     * @param sysMenu 字典
     * @return {@code 0} 添加成功，{@code 500} 添加失败
     */
    @Operation(summary = "新增菜单", description = "新增菜单")
    @ApiResponse(responseCode = "2xx",description = "新增菜单")
    @PostMapping("/save")
    public ReturnT<Boolean> save(@Validated @RequestBody SysMenu sysMenu) {
        try{
            sysMenu.setId(UuidUtil.get32UUID());
            return ReturnT.ok(sysMenuService.save(sysMenu));
        }catch(Exception e){
            log.error("保存菜单失败", e);
            return ReturnT.error("保存菜单失败！");
        }
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code 0} 删除成功，{@code 500} 删除失败
     */
    @Operation(summary = "根据主键删除菜单", description = "根据主键删除菜单")
    @ApiResponse(responseCode = "2xx",description = "根据主键删除菜单")
    @DeleteMapping("/remove/{id}")
    public ReturnT<Boolean> remove(@PathVariable(name="id") Serializable id) {
        try{
            return ReturnT.ok(sysMenuService.removeById(id));
        }catch(Exception e){
            log.error("删除菜单失败", e);
            return ReturnT.error("删除菜单失败！");
        }
    }
    
    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code 0} 删除成功，{@code 500} 删除失败
     */
    @Operation(summary = "根据主键删除菜单", description = "根据主键删除菜单")
    @ApiResponse(responseCode = "2xx",description = "根据主键删除菜单")
    @GetMapping("/delete/{id}")
    public ReturnT<Boolean> delete(@PathVariable(name="id") Serializable id) {
        try{
            return ReturnT.ok(sysMenuService.removeById(id));
        }catch(Exception e){
            log.error("删除菜单失败", e);
            return ReturnT.error("删除菜单失败！");
        }
    }
    
    /**
     * 根据主键更新。
     *
     * @param sysMenu 字典
     * @return {@code 0} 更新成功，{@code 500} 更新失败
     */
    @Operation(summary = "根据主键更新菜单", description = "根据主键更新菜单")
    @ApiResponse(responseCode = "2xx",description = "根据主键更新菜单")
    @PostMapping("/update")
    public ReturnT<Boolean> update(@Validated @RequestBody SysMenu sysMenu) {
        try{
            return ReturnT.ok(sysMenuService.updateById(sysMenu));
        }catch(Exception e){
            log.error("修改菜单失败", e);
            return ReturnT.error("修改菜单失败！");
        }
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @Operation(summary = "根据主键获取详细信息", description = "根据主键获取详细信息")
    @ApiResponse(responseCode = "2xx",description = "根据主键获取详细信息")
    @GetMapping("/getInfo/{id}")
    public ReturnT<SysMenu> getInfo(@PathVariable(name="id") Serializable id) {
        return ReturnT.ok(sysMenuService.getById(id));
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @Operation(summary = "分页查询菜单", description = "分页菜单")
    @ApiResponse(responseCode = "2xx",description = "分页查询菜单")
    @PostMapping("/page")
    public ReturnT<Page<SysMenu>> page(@RequestBody Page<SysMenu> page) {
        return ReturnT.ok(sysMenuService.page(page));
    }
    
    /**
     * 查询树形菜单
     * @return 树形菜单
     */
    @Operation(summary = "查询树形菜单", description = "树形菜单")
    @ApiResponse(responseCode = "2xx",description = "查询树形菜单")
    @GetMapping("/getMenuList")
    public ReturnT<List<SysMenu>> getMenuList(){
        List<SysMenu> list = sysMenuService.getMenuList();
        return ReturnT.ok(list);
    }
}
